using UnityEngine;
using System.Collections;

[System.Serializable]
public class TimeRecordAchievement : AchievementItem{
	
	public int NUM_fases;
	public float unlock_time;
	private string TAG = "Time Record Achievement";
	public float[] tempos;
	
	public string completed_key = "TimeRecordAchievement_Completed";
	public string save_key = "TimeRecordAchievement";
	
	public float timeAdded = 0; 
		
	
	public void Setup()
	{
		this.tempos = new float[NUM_fases];
		
		TrySetStadiumTime(5, 20f);
		
		Load();
		
		int tmpCompleted = PlayerPrefs.GetInt(this.completed_key);
		if(tmpCompleted == 1)
		{
			SetCompleted();
		}
	}
	public void TrySetStadiumTime (int stadium, float time)
	{
		if(this.tempos[stadium] < time){
			this.tempos[stadium] = time;
			
			Save(this.tempos);
		}else
		{
			Debug.Log(TAG + ": Tempo de fase menor que o recorde atual.");
		}
	}
	
	public bool Teste_UnlockAchieve()
	{
		float tmpSumTime = 0;
		foreach(float f in tempos)
		{	
			tmpSumTime += f;
			if(tmpSumTime >= unlock_time)
			{
				Debug.Log("Teste Achievement: " + this.name + ": liberado.");
				SetCompleted();
				return true;
			}
		}
		return false;
	}
	
	public void Save(float[] tempos)
	{
		for (int i = 0; i < this.NUM_fases; i++) {
		
			PlayerPrefs.SetFloat(this.save_key + i.ToString(), this.tempos[i]);
		}
		Debug.Log(TAG + ": SAVE SUCCESS");
	}
	
	public void Load()
	{

		for (int i = 0; i < this.NUM_fases; i++) {
			
			this.tempos[i] = PlayerPrefs.GetFloat(this.save_key + i.ToString());
		}
		Debug.Log(TAG + ": LOAD SUCCESS");
	}

	public void SetCompleted()
	{
		this.completed = true;
		PlayerPrefs.SetInt(this.completed_key, 1);
		AchieveStorage.getInstance.getByName(this.name).SetCompleted();
		Debug.Log(TAG + ": Set Completed");	
	}

}

